package com.wolfalpha.utron.net;

import java.io.Serializable;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * 接口URL实体
 *
 * @version 1.0
 * @created 2012-3-21
 */
public class URLs implements Serializable {

    //  public final static String HOST = "139.196.17.95/Utron/App";//服务器1 aliyun
    public final static String HOST = "117.78.44.167/Utron/App";//服务器2 hwcloud

    //    public final static String HOST = "192.168.1.103/Utron/App"; //局域网测试
    public final static String HTTP = "http://";
    public final static String HTTPS = "https://";

    private final static String URL_SPLITTER = "/";
    private final static String URL_UNDERLINE = "_";

    private final static String URL_API_HOST = HTTP + HOST + URL_SPLITTER;
    //登录
    public final static String LOGIN_VALIDATE_HTTP = HTTP + HOST + URL_SPLITTER + "sign.class.php?act=login";
    public final static String LOGIN_VALIDATE_HTTPS = HTTPS + HOST + URL_SPLITTER + "sign.class.php?act=login";
    //匿名登录
    public final static String ANONLOGIN_VALIDATE_HTTP = HTTP + HOST + URL_SPLITTER + "sign.class.php?act=anonlogin";
    //获取个人身份信息
    public final static String USER_GET_INDENTITY = URL_API_HOST + "user.class.php?act=getIdentity";
    //获取验证码
    public final static String PHONE_GET_CODE = URL_API_HOST + "phone.class.php?act=getCode&phone=";
    //验证验证码
    public final static String PHONE_CHECK_CODE = URL_API_HOST + "phone.class.php?act=check";
    //判断昵称是否被注册
    public final static String USER_CHECK_NICKNAME = URL_API_HOST + "user.class.php?act=onlynick&nickname=";
    //注册
    public final static String USER_REGISTER = URL_API_HOST + "sign.class.php?act=register";
    //获取一条需求
    public final static String REQUIRE_GET_ONE = URL_API_HOST + "requires.class.php?act=getOne";
    //获取个人所有需求
    public final static String REQUIRE_GET_LIST = URL_API_HOST + "requires.class.php?act=getList";
    //发布一条需求
    public final static String REQUIRE_PUBLISH_ONE = URL_API_HOST + "requires.class.php?act=publish";
    //更新一条需求
    public final static String REQUIRE_DO_ONE = URL_API_HOST + "requires.class.php?act=do";
    //更新头像
    public final static String USER_UPDATE_HEAD = URL_API_HOST + "user.class.php?act=updateHead";
    //用户接口
    public final static String USER_URL = URL_API_HOST + "user.class.php?act=";
    //需求接口
    public final static String REQUIRE_URL = URL_API_HOST + "requires.class.php?act=";
    //资讯接口
    public final static String INFORMATION_URL = URL_API_HOST + "information.class.php?act=";
    //想法接口
    public final static String IDEA_URL = URL_API_HOST + "idea.class.php?act=";
    //项目接口
    public final static String PROJECT_URL = URL_API_HOST + "project.class.php?act=";
    //文件接口
    public final static String FILE_URL = URL_API_HOST + "file.class.php?act=";
    //合伙人接口
    public final static String PARTNER_URL = URL_API_HOST + "partner.class.php?act=";
    //活动接口
    public final static String ACTIVITY_URL = URL_API_HOST + "activity.class.php?act=";
    //全局接口
    public final static String APP_URL = URL_API_HOST + "app.class.php?act=";
    //好友接口
    public final static String FRIENDS_URL = URL_API_HOST + "friends.class.php?act=";


    public final static int URL_OBJ_TYPE_OTHER = 0x000;
    public final static int URL_OBJ_TYPE_NEWS = 0x001;

    private int objId;
    private String objKey = "";
    private int objType;

    public int getObjId() {
        return objId;
    }

    public void setObjId(int objId) {
        this.objId = objId;
    }

    public String getObjKey() {
        return objKey;
    }

    public void setObjKey(String objKey) {
        this.objKey = objKey;
    }

    public int getObjType() {
        return objType;
    }

    public void setObjType(int objType) {
        this.objType = objType;
    }

//    /**
//     * 转化URL为URLs实体
//     * @param path
//     * @return 不能转化的链接返回null
//     */
//    public final static URLs parseURL(String path) {
//        if(StringUtils.isEmpty(path))return null;
//        path = formatURL(path);
//        URLs urls = null;
//        String objId = "";
//        try {
//            URL url = new URL(path);
//            //站内链接
//            if(url.getHost().contains(URL_HOST)){
//                urls = new URLs();
//                //www
//                if(path.contains(URL_WWW_HOST )){
//                    //新闻  www.oschina.net/news/27259/mobile-internet-market-is-small
//                    if(path.contains(URL_TYPE_NEWS)){
//                        objId = parseObjId(path, URL_TYPE_NEWS);
//                        urls.setObjId(StringUtils.toInt(objId));
//                        urls.setObjType(URL_OBJ_TYPE_NEWS);
//                    }
//                    //软件  www.oschina.net/p/jx
//                    else if(path.contains(URL_TYPE_SOFTWARE)){
//                        urls.setObjKey(parseObjKey(path, URL_TYPE_SOFTWARE));
//                        urls.setObjType(URL_OBJ_TYPE_SOFTWARE);
//                    }
//                    //问答
//                    else if(path.contains(URL_TYPE_QUESTION)){
//                        //问答-标签  http://www.oschina.net/question/tag/python
//                        if(path.contains(URL_TYPE_QUESTION_TAG)){
//                            urls.setObjKey(parseObjKey(path, URL_TYPE_QUESTION_TAG));
//                            urls.setObjType(URL_OBJ_TYPE_QUESTION_TAG);
//                        }
//                        //问答  www.oschina.net/question/12_45738
//                        else{
//                            objId = parseObjId(path, URL_TYPE_QUESTION);
//                            String[] _tmp = objId.split(URL_UNDERLINE);
//                            urls.setObjId(StringUtils.toInt(_tmp[1]));
//                            urls.setObjType(URL_OBJ_TYPE_QUESTION);
//                        }
//                    }
//                    //other
//                    else{
//                        urls.setObjKey(path);
//                        urls.setObjType(URL_OBJ_TYPE_OTHER);
//                    }
//                }
//                //my
//                else if(path.contains(URL_MY_HOST)){
//                    //博客  my.oschina.net/szpengvictor/blog/50879
//                    if(path.contains(URL_TYPE_BLOG)){
//                        objId = parseObjId(path, URL_TYPE_BLOG);
//                        urls.setObjId(StringUtils.toInt(objId));
//                        urls.setObjType(URL_OBJ_TYPE_BLOG);
//                    }
//                    //动弹  my.oschina.net/dong706/tweet/612947
//                    else if(path.contains(URL_TYPE_TWEET)){
//                        objId = parseObjId(path, URL_TYPE_TWEET);
//                        urls.setObjId(StringUtils.toInt(objId));
//                        urls.setObjType(URL_OBJ_TYPE_TWEET);
//                    }
//                    //个人专页  my.oschina.net/u/12
//                    else if(path.contains(URL_TYPE_ZONE)){
//                        objId = parseObjId(path, URL_TYPE_ZONE);
//                        urls.setObjId(StringUtils.toInt(objId));
//                        urls.setObjType(URL_OBJ_TYPE_ZONE);
//                    }
//                    else{
//                        //另一种个人专�? my.oschina.net/dong706
//                        int p = path.indexOf(URL_MY_HOST+URL_SPLITTER) + (URL_MY_HOST+URL_SPLITTER).length();
//                        String str = path.substring(p);
//                        if(!str.contains(URL_SPLITTER)){
//                            urls.setObjKey(str);
//                            urls.setObjType(URL_OBJ_TYPE_ZONE);
//                        }
//                        //other
//                        else{
//                            urls.setObjKey(path);
//                            urls.setObjType(URL_OBJ_TYPE_OTHER);
//                        }
//                    }
//                }
//                //other
//                else{
//                    urls.setObjKey(path);
//                    urls.setObjType(URL_OBJ_TYPE_OTHER);
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            urls = null;
//        }
//        return urls;
//    }

    /**
     * 解析url获得objId
     *
     * @param path
     * @param url_type
     * @return
     */
    private final static String parseObjId(String path, String url_type) {
        String objId = "";
        int p = 0;
        String str = "";
        String[] tmp = null;
        p = path.indexOf(url_type) + url_type.length();
        str = path.substring(p);
        if (str.contains(URL_SPLITTER)) {
            tmp = str.split(URL_SPLITTER);
            objId = tmp[0];
        } else {
            objId = str;
        }
        return objId;
    }

    /**
     * 解析url获得objKey
     *
     * @param path
     * @param url_type
     * @return
     */
    private final static String parseObjKey(String path, String url_type) {
        path = URLDecoder.decode(path);
        String objKey = "";
        int p = 0;
        String str = "";
        String[] tmp = null;
        p = path.indexOf(url_type) + url_type.length();
        str = path.substring(p);
        if (str.contains("?")) {
            tmp = str.split("\\?");
            objKey = tmp[0];
        } else {
            objKey = str;
        }
        return objKey;
    }

    /**
     * 对URL进行格式处理
     *
     * @param path
     * @return
     */
    private final static String formatURL(String path) {
        if (path.startsWith("http://") || path.startsWith("https://"))
            return path;
        return "http://" + URLEncoder.encode(path);
    }
}